<?php

namespace Qy\Controller;

use Qy\Controller\QyBaseController;

/**
 * 学习中心-公开课
 */
class PublicCurrController extends QyBaseController {

    /**
     *  公开课列表
     */
    public function index() {
        $page_no = I('page_no', 1, 'intval');
        
        $Public = D('Public');
        $publicCurr = $Public->table('qy_public c')
                            ->field("c.public_id,c.public_name,c.public_img,c.teacher_id,c.description,c.public_url,c.play_code,c.public_url,c.aliyun_video_id,c.file_url,c.cate_ids,c.start_time,c.end_time,t.teacher_name")
                            ->join("qy_teacher t USING(teacher_id)")
                            ->order('start_time desc')
                            ->limit(5)
                            ->page($page_no)
                            ->select();
        
        $count = $Public->count(); //总条数
        $strUserName = M('User')->where(array('user_id'=>$this->iUserId))->getField('user_name');
        $CategoryAll = M('CategoryAll')->field('cate_id,cate_name')->where(array('type' => 1))->select();
        $CategoryAll = array_column($CategoryAll, 'cate_name', 'cate_id');
        $time = date('Y-m-d H:i:s', time());
        foreach ($publicCurr as &$val) {
            //岗位名称
            $val['is_Privilege'] = 0;
            $val['cate_ids'] = explode(',', $val['cate_ids']);
            if (in_array($this->iPostId, $val['cate_ids']) || $this->iRoleId == 1 || $this->iRoleId == 2) {
                $val['is_Privilege'] = 1;
            }

            foreach ($val['cate_ids'] as &$v) {
                $v = $CategoryAll[$v];
            }
            //封面图
            $val['public_img'] = C('ADMIN_URL') . $val['public_img'];
            if(!empty($val['play_code'])){
                $val['public_url'] = $val['public_url'].'?nickName='.$strUserName.'&token='.$val['play_code'];
            } 
            //播放状态
            if ($val['start_time'] > $time) {
                $val['status'] = 1;    //即将直播
            } else if ($val['start_time'] <= $time && $val['end_time'] >= $time) {
                $val['status'] = 2;    //进入直播
            } else if ($val['ent_time'] <= $time && empty($val['aliyun_video_id'])) {
                $val['status'] = 3;    //直播结束
            } else if ($val['ent_time'] <= $time && !empty($val['aliyun_video_id'])) {
                $val['status'] = 4;    //查看录播
            }
        }
        $this->returnList($publicCurr, $page_no, 5, $count);
    }

    /**
     *  详情
     */
    public function detail() {
        $objectPublic = D('Public');
        $strPublic_id = I('public_id');
        if (empty($strPublic_id)) {
            $this->error("参数错误！");
        }

        $publicInfo = $objectPublic
                ->field("public_name,public_img,teacher_id,description,aliyun_video_id,cate_ids,start_time,end_time")
                ->where(array('public_id' => $strPublic_id))
                ->find();

        if (empty($publicInfo['aliyun_video_id'])) {
            $this->error("视频暂未开放");
        }

        $arrCateIds = explode(',', $publicInfo['cate_ids']);
        if (!in_array($this->iPostId, $arrCateIds) && $this->iRoleId = 0) {
            $this->error("没有查看该公开课的权限");
        }
        $publicInfo['public_img'] = C('ADMIN_URL') . $publicInfo['public_img'];

        $this->returnList($publicInfo);
    }

    /**
     *  统计经验点
     */
    public function UserIntegral() {
        $public_id = I('public_id', 0, 'intval');
        $cate_ids = D('Public')->where(array('public_id' => $public_id))->getField("cate_ids");

        if (in_array($this->iPostId, $cate_ids) || $this->iRoleId == 1) {
            $UserIntegralLogModel = new \Common\Model\UserIntegralLogModel();
            $UserIntegralLogModel->addUserIntegral($this->iUserId, 2, $public_id);
        }
    }

}
